package com.travelsky.airchina.boarding.web.admin;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.travelsky.airchina.boarding.domain.Resource;
import com.travelsky.airchina.boarding.service.ResourceService;
import com.travelsky.airchina.boarding.web.validator.ResourceValidator;
/**
 * 资源权限控制类
 * @author haichenwang
 *
 */
@Controller
@RequestMapping(value="/system/resource")
public class ResourceController {
	@Autowired
	private ResourceService resourceService;
	/**
	 * 转到创建资源权限页面
	 * @param model
	 * @return
	 */
	@RequestMapping(method=RequestMethod.GET)
	public String toCreate(Model model) {
		List<Resource> resourceList = resourceService.findResourcesByParentId(null);
		model.addAttribute(resourceList);
		Resource resource = new Resource();
		resource.setSortOrder(resourceService.getResourceSum());
		model.addAttribute(resource);
		return "system/resource/createForm";
	}
	/**
	 * 创建资源权限
	 * @param user
	 * @param result
	 * @return
	 */
	@RequestMapping(method=RequestMethod.POST)
	public String create(@ModelAttribute("resource") Resource resource, BindingResult result,Model model,HttpServletRequest request) {
		resource.setIsLeaf("1");
		resource.setStatus("1");
		resource.setType("url");
		String parentResourceId = request.getParameter("p_cate_id");
		if(null!=parentResourceId && !"-1".equals(parentResourceId) && !"".equals(parentResourceId.trim())){
			Resource parentResource = new Resource();
			parentResource.setId(new Long(parentResourceId));
			resource.setParentResource(parentResource);
		}
		new ResourceValidator().validate(resource,resourceService,result);
		if (result.hasErrors()) {
			return "system/resource/createForm";
		}
		resourceService.saveResource(resource);
		return "redirect:/system/resource/"+resource.getId();
	}
	/**
	 * 资源权限列表首页
	 * @param id 父资源权限的主键
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/manage", method=RequestMethod.GET)
	public String manage(Model model) {
		List<Resource> resources = resourceService.findResourcesByParentId(null);
		model.addAttribute(resources);
		model.addAttribute("manage","manage");
		return "system/resource/resourceList";
	}
	/**
	 * 查看新闻列表
	 * @param id 父资源权限的主键
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/list/{id}", method=RequestMethod.GET)
	public String list(@PathVariable Long id, Model model) {
		List<Resource> resources = null;
		resources = resourceService.findResourcesByParentId(id);
		model.addAttribute(resources);
		Resource resource = resourceService.getResource(id);
		Long parentId = null;
		if(resource!=null && resource.getParentResource()!=null){
			parentId = resource.getParentResource().getId();
		}
		model.addAttribute("parentId", parentId);
		return "system/resource/resourceList";
	}
	/**
	 * 转到修改资源权限页面
	 * @param id 资源权限的主键
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/update/{id}", method=RequestMethod.GET)
	public String toUpdate(@PathVariable Long id, Model model) {
		Resource resource = resourceService.getResource(id);
		List<Resource> rootResourceTemps = resourceService.findResourcesByParentId(null);
		
		List<Resource> rootResources = new ArrayList<Resource>();
		if(rootResourceTemps!=null && rootResourceTemps.size()>0){
			for(Resource temp:rootResourceTemps){
				if(!temp.getId().equals(id)&&temp.getParentResource()==null){
					rootResources.add(temp);
				}
			}
		}
		List<Resource> parentResources = resourceService.findAllParentResourcesByChildId(id);
		List<Long> resourceIds = new ArrayList<Long>();
		Map<Long,List<Resource>> map = null;
		if(parentResources!=null && parentResources.size()>0){
			for(Resource temp:parentResources){
				resourceIds.add(temp.getId());
			}
			map = resourceService.findChildrenResourceByParentId(resourceIds,id);
			Long rootResourceId = resourceIds.get(0);
			model.addAttribute("rootResourceId",rootResourceId);
		}else{
			map = new HashMap<Long,List<Resource>>();
		}
		model.addAttribute("rootResources",rootResources);
		model.addAttribute(resource);
		model.addAttribute("map",map);
		model.addAttribute("resourceIds",resourceIds);
		return "system/resource/updateForm";
	}
	/**
	 * 修改资源权限
	 * @param resource
	 * @param oldParentResourceId
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/update", method=RequestMethod.POST)
	public String update(@ModelAttribute("resource") Resource resource,@RequestParam String oldParentResourceId,String p_cate_id,  Model model) {
		Long oldParentResourceIdL = null;
		if(oldParentResourceId!=null && !"-1".equals(oldParentResourceId) && !"".equals(oldParentResourceId)){
			oldParentResourceIdL = new Long(oldParentResourceId);
		}
		if(null!=p_cate_id && !"-1".equals(p_cate_id)){
			Resource parentResource = new Resource();
			parentResource.setId(new Long(p_cate_id));
			resource.setParentResource(parentResource);
		}
		resourceService.updateResource(resource, oldParentResourceIdL);
		model.addAttribute(resource);
		return "redirect:/system/resource/"+resource.getId();
	}
	/**
	 * 查看资源权限信息
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value="{id}", method=RequestMethod.GET)
	public String getView(@PathVariable Long id, Model model) {
		Resource resource = resourceService.getResource(id);
		List<Resource> rootResourceTemps = resourceService.findResourcesByParentId(null);
		List<Resource> rootResources = new ArrayList<Resource>();
		if(rootResourceTemps!=null){
			for(Resource temp:rootResourceTemps){
				if(!temp.getId().equals(id)){
					rootResources.add(temp);
				}
			}
		}
		List<Resource> parentResources = resourceService.findAllParentResourcesByChildId(id);
		List<Long> resourceIds = new ArrayList<Long>();
		Map<Long,List<Resource>> map = null;
		if(parentResources!=null && parentResources.size()>0){
			for(Resource temp:parentResources){
				if(temp.getParentResource()==null){
					resourceIds.add(temp.getId());
				}
			}
			if(resourceIds.size()>0){
				map = resourceService.findChildrenResourceByParentId(resourceIds,id);
				Long rootResourceId = resourceIds.get(0);
				model.addAttribute("rootResourceId",rootResourceId);
			}
		}else{
			map = new HashMap<Long,List<Resource>>();
		}
		model.addAttribute("rootResources",rootResources);
		model.addAttribute(resource);
		model.addAttribute("rootResources",rootResources);
		model.addAttribute("map",map);
		model.addAttribute("resourceIds",resourceIds);
		return "system/resource/view";
	}
	/**
	 * 通过ajax根据父亲主键获得其下一级栏目
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/childresource", method=RequestMethod.GET)
	public @ResponseBody List<Resource> findChildren(@RequestParam Long id) {
		List<Resource> resources = resourceService.findResourcesByParentId(id);
		return resources;
	}
}
